﻿#region

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using HouseHoldBudget.App_GlobalResources;
using HouseHoldBudget.Core.Entities.Budgets;
using HouseHoldBudget.Infrastructure;
using HouseHoldBudget.Models.User;

#endregion

namespace HouseHoldBudget.Models.Budgets
{
    public class BudgetResultModel : BudgetModel
    {
        #region Instance Properties

        /// <summary>
        ///   Gets or sets the annual period.
        /// </summary>
        /// <value> The annual period. </value>
        public int AnnualPeriod { get; set; }

        /// <summary>
        ///   Gets or sets the annual period items.
        /// </summary>
        /// <value> The annual period items. </value>
        [ResourceDisplayName("Common_Period")]
        public IList<SelectListItem> AnnualPeriodItems { get; set; }

        /// <summary>
        ///   Gets or sets the month period.
        /// </summary>
        /// <value> The month period. </value>
        public int MonthPeriod { get; set; }

        /// <summary>
        ///   Gets or sets a value indicating whether [month period enabled].
        /// </summary>
        /// <value> <c>true</c> if [month period enabled]; otherwise, <c>false</c> . </value>
        public bool MonthPeriodEnabled
        {
            get
            {
                return (BudgetTypes)BudgetTypeId == BudgetTypes.Mensual;
            }
        }

        /// <summary>
        ///   Gets or sets the month period items.
        /// </summary>
        /// <value> The month period items. </value>
        [ResourceDisplayName("Common_Period")]
        public IList<SelectListItem> MonthPeriodItems { get; set; }


        /// <summary>
        ///   Gets the total expenses.
        /// </summary>
        /// <value> The total expenses. </value>
        [ResourceDisplayName("BudgetSummary_TotalExpenses")]
        [DataType(DataType.Currency)]
        public decimal TotalExpenses
        {
            get
            {
                //return 0;
                decimal myExpenses = BudgetOwner.Expenses.Sum(p => p.ExpenseAmount);
                decimal actorsExpenses = BudgetActors.Sum(u => u.Expenses.Sum(p => p.ExpenseAmount));
                return myExpenses + actorsExpenses;
            }
        }

        /// <summary>
        ///   Gets the total incomes.
        /// </summary>
        /// <value> The total incomes. </value>
        [ResourceDisplayName("BudgetSummary_TotalIncomes")]
        [DataType(DataType.Currency)]
        public decimal TotalIncomes
        {
            get
            {
               // return 0;
                decimal myIncomes = BudgetOwner.TotalIncomesAmount;
                decimal actorsIncomes = BudgetActors.Sum(u => u.FixedIncomes.Sum(p => p.FixedIncomesAmount)) + BudgetActors.Sum(u => u.OccasionnalIncomes.Sum(p => p.OccasionalIncomesAmount));
                return myIncomes + actorsIncomes;
            }
        }

        /// <summary>
        ///   Gets the total outstanding.
        /// </summary>
        /// <value> The total outstanding. </value>
        [ResourceDisplayName("BudgetSummary_TotalOutstanding")]
        [DataType(DataType.Currency)]
        public decimal TotalOutstanding
        {
            get
            {
                return TotalIncomes - TotalExpenses;
            }
        }

        #endregion

        public BudgetResultModel()
        {
            MonthPeriodItems = new List<SelectListItem>();
            AnnualPeriodItems = new List<SelectListItem>();
            BudgetOwner = new BudgetUserModel();
            
        }

        public void PrepareMonthPeriodItems()
        {
            MonthPeriodItems.Add(new SelectListItem()
            {
                Text = CustomResources.Common_Month,
                Value = "0",
            });
            for (int i = 1; i <= 12; i++)
            {
                MonthPeriodItems.Add(new SelectListItem()
                                         {
                                             Text = i.ToString(),
                                             Value = i.ToString(),
                                         });
            }
        }

        public void PrepareYearPeriodItems()
        {
            AnnualPeriodItems.Add(new SelectListItem()
            {
                Text = CustomResources.Common_Annual,
                Value = "0",
            });

            AnnualPeriodItems.Add(new SelectListItem()
            {
                Text = DateTime.Now.AddYears(-1).Year.ToString(),
                Value = DateTime.Now.AddYears(-1).Year.ToString(),
            });

            AnnualPeriodItems.Add(new SelectListItem()
            {
                Text = DateTime.Now.Year.ToString(),
                Value = DateTime.Now.Year.ToString(),
            });

            AnnualPeriodItems.Add(new SelectListItem()
            {
                Text = DateTime.Now.AddYears(1).Year.ToString(),
                Value = DateTime.Now.AddYears(1).Year.ToString(),
            });

        }
    }
}